#include "StdAfx.h"
#include "SocketException.h"

SocketException::SocketException(uint32 errorCode, const std::string& function) {
#ifdef WIN32
	LPSTR message = NULL;
	if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER, NULL, errorCode, LANG_SYSTEM_DEFAULT, (LPSTR)&message, 0, NULL) > 0) {
		LPSTR nl = strchr(message, '\r');
		if(nl != NULL)
			*nl = '\0';

		std::stringstream strm;
		if(function == "")
			strm << "An exception using a Winsock2 function occured:" << std::endl << message;
		else
			strm << "An exception using the Winsock2 function '" << function << "' occured:" << std::endl << message;

		mMessage = strm.str();
	} else {
		std::stringstream strm;
		if(function == "")
			strm << "An exception using a Winsock2 function occured. The error code is not recognized: 0x" << std::hex << errorCode;
		else
			strm << "An exception using the Winsock2 function '" << function << "' occured. The error code is not recognized: 0x" << std::hex << errorCode;
		mMessage = strm.str();
	}
#else
#endif
}